<?php
namespace Sys\Controller;
use Todo\Controller\ApiController;

class RoleController extends ApiController {

    public function validateCode(){
        $value = I('get.code');
        if(!$value) die('false');

        $this->_validate('code', $value, 'unique');
    }

    /**
     * 为角色设置菜单
     */
    public function menu(){
        $roleId = I('post.role_id');
        if(!$roleId) $this->apiError('参数不完整');

        $this->_loadModel();
        $this->_model->startTrans();
        $ret = D('Todo/Sys/RoleMenu')->where(array('role_id'=>$roleId))->delete();
        if($ret === false) {
            $this->_model->rollback();
            $this->apiError('操作失败1');
        }

        $menuIds = I('post.menu_id');
        if($menuIds){
            $menuArr = explode(',', $menuIds);
            $list = array();
            foreach ($menuArr as $m){
                if(strpos('app_', $m) !== false) continue;
                $list[] = array(
                    'menu_id' => $m,
                    'role_id' => $roleId
                );
            }
            $ret = D('Todo/Sys/RoleMenu')->addAll($list);
            if($ret === false) {
                $this->_model->rollback();
                $this->apiError('操作失败1');
            }
        }

        $this->_model->commit();
        $this->apiSuccess(null, '操作成功');
    }
}